package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.15
 * @since 1.0
 */
@RestController
@RequestMapping("/rest")
//@CrossOrigin(value = {"http://localhost:9090","http://127.0.0.1:9090"})
public class RestfulController {

    List<User> list = new ArrayList<>();

    {
        list.add(new User(1,"zhangsan","123",18,"男",true,new Date()));
        list.add(new User(2,"lisi","1234",19,"女",true,new Date()));
        list.add(new User(3,"wangwu","12345",20,"男",true,new Date()));
        list.add(new User(4,"maliu","123456",21,"女",true,new Date()));
    }

   // @RequestMapping(value = "/users",method = RequestMethod.GET)
    @GetMapping("/users")
    public List<User> getUsers(){
        //真实开发:从数据库中获取所有的User信息
        return list;
    }

   // @RequestMapping(value = "/users",method = RequestMethod.POST)
    @PostMapping("/users")
    public String saveUser(@RequestBody User user){
        list.add(user);
        System.out.println("post"+user);
        return "success";
    }

   // @RequestMapping(value = "/users",method = RequestMethod.PUT)
    @PutMapping("/users")
    public String putUser(@RequestBody User user){
        list.add(user);
        System.out.println("put"+user);
        return "success";
    }

    //@PathVariable   就是把路径中的参数映射到方法参数中
    @DeleteMapping("/users/{id}")
   // @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id){
        System.out.println("delete:" + id );
        return "success";
    }
}

